/**
 * @Author YuanYuan
 * @Date 2022/7/22
 * @Time 14:37
 */
public class BinarySearchTree {

    static class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int key) {
            this.key = key;
        }
    }

    public TreeNode root;

    /**
     * 插入一个元素
     * @param key
     */
    public boolean insert(int key) {
        if(null == root) {
            root = new TreeNode(key);
            return true;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while(null != cur) {
            if(cur.key>key) {
                parent = cur;
                cur = cur.left;
            }else if(cur.key<key) {
                parent = cur;
                cur = cur.right;
            }else {
                return false;
            }
        }
        TreeNode node = new TreeNode(key);
        if(parent.key<key) {
            parent.right = node;
        }else if(parent.key>key) {
            parent.left = node;
        }else{
            return false;
        }
        return true;
    }
    //查找key是否存在
    public TreeNode search(int key) {
        if(null == root) {
            return null;
        }
        TreeNode cur = root;
        while(null!=cur) {
            if(cur.key>key) {
                cur = cur.left;
            }else if(cur.key<key) {
                cur = cur.right;
            }else {
                return cur;
            }
        }
        return null;
    }

    /**
     * @param
     * @return boolean
     * 找到要删除值的位置
    */
    public boolean remove(int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        while(null!=cur) {
            if(cur.key>key) {
                parent = cur;
                cur = cur.left;
            }else if(cur.key<key) {
                parent = cur;
                cur = cur.right;
            }else {
                removeNode(cur,parent);
                return true;
            }
        }
        return false;
    }
    //删除key的值
    private void removeNode(TreeNode cur,TreeNode parent) {
        if(null == cur.right) {
            if(cur == parent.left) {
                parent.left = cur.left;
            }else if(cur == parent.right) {
                parent.right = cur.left;
            }else if(cur == root) {
                root = root.left;
            }
        }else if(null == cur.left) {
            if(cur == parent.left) {
                parent.left = cur.right;
            }else if(cur == parent.right) {
                parent.right = cur.right;
            }else if(cur == root) {
                root = root.right;
            }
        }else {
            if(cur == root) {
                cur = root.right;
                parent = root;
                //找到右树最左边的叶子节点
                while(null!=cur&&null!=cur.left) {
                    parent = cur;
                    cur = cur.left;
                }
                //对值进行替换
                root.key = cur.key;
                //如果要替换的位置就是右树的结点,并且右树左右都为空,直接右树置为空
                if(root == parent&&null ==cur.left&&null == cur.right) {
                    parent.right = null;
                }else if (root == parent&&null != cur.right&&null == cur.left) {
                    //如果要替换的位置就是右树的结点,左树为空右树不为空,右树继承下去
                    parent.right = cur.right;
                }else {
                    //替换的位置不是右树
                    parent.left = null;
                }

            }else {
                if(cur == parent.left) {
                    parent.left = cur.right;
                    parent.left.left = cur.left;
                }else if(cur == parent.right) {
                    parent.right = cur.right;
                    parent.right.left = cur.left;
                }
            }
     }
    }
    //中序遍历
    public void inOrder(TreeNode root) {
        if(null == root) {
            return;
        }
        inOrder(root.left);
        System.out.println(root.key);
        inOrder(root.right);
    }



}